<template>
  <div>
    <el-dialog v-bind="$attrs" v-on="$listeners" @open="onOpen" @close="onClose" title="Dialog Titile">
      <el-form ref="elForm" :model="relationMapping" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="用户一" prop="userFirstId">
          <el-select v-model="relationMapping.userFirstId" placeholder="请选择用户一" clearable
                     :style="{width: '100%'}">
            <el-option v-for="(item, index) in students" :key="index" :label="item.nickName"
                       :value="item.userId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户二" prop="userSecondId">
          <el-select v-model="relationMapping.userSecondId" placeholder="请选择用户二" clearable
                     :style="{width: '100%'}">
            <el-option v-for="(item, index) in students" :key="index" :label="item.nickName"
                       :value="item.userId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户关系" prop="relationId">
          <el-select v-model="relationMapping.relationId" placeholder="请选择用户关系" clearable
                     :style="{width: '100%'}">
            <el-option v-for="(item, index) in relations" :key="index" :label="item.relationName"
                       :value="item.relationId"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {updateRelationMapping} from "@/api/relation";

export default {
  inheritAttrs: false,
  components: {},
  props:{students:[],relations:[],relationMapping:{}},
  data() {
    return {
      rules: {
        userFirstId: [{
          required: true,
          message: '请选择用户一',
          trigger: 'change'
        }],
        userSecondId: [{
          required: true,
          message: '请选择用户二',
          trigger: 'change'
        }],
        relationId: [{
          required: true,
          message: '请选择用户关系',
          trigger: 'change'
        }],
      }
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    onOpen() {},
    onClose() {
      this.$refs['elForm'].resetFields()
    },
    close() {
      this.$emit('update:visible', false)
    },
    handelConfirm() {
      this.$refs['elForm'].validate(valid => {
        if (!valid) return
        if (this.relationMapping.userFirstId===this.relationMapping.userSecondId){
          this.$message.error("两个用户不能相同"); return;
        }
        updateRelationMapping(this.relationMapping).then(res=>{
          if (res.code===200){
            this.$message.success("更新成功")
            this.$emit("addResult")
          }
        })
        this.close()
      })
    },
  }
}

</script>
<style>
</style>
